//原文链接：
//@https://blog.csdn.net/ITSOK_123/article/details/124427555
//方法1：（左闭右闭区间）
class Solution {
public:
    int search(vector<int>& nums, int target) {
        int left =0,right=nums.size()-1;
        while(left<=right){
            int mid = left+(right-left)/2;
            if(target<nums[mid]){
                right = mid-1;
            }
            else if(target>nums[mid]){
                left = mid+1;
            }
            else return mid;
        }
        return -1;
    }
};

//方法2：（左闭右开区间）
class Solution {
public:
    int search(vector<int>& nums, int target) {
        int left =0,right=nums.size();
        while(left<right){
            int mid = left+(right-left)/2;
            if(target<nums[mid]){   
                right = mid;
            }
            else if(target>nums[mid]){
                left = mid+1;
            }
            else return mid;
        }
        return -1;
    }
};